package com.djf.djfword.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Numberings;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.data.Tables;
import com.deepoove.poi.data.style.BorderStyle;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 26393
 * @Date 2025/7/8 23:47
 * @Version 1.0
 */
@RestController
public class WordExportController {

    @GetMapping("/exportWord")
    public void exportWord(HttpServletResponse response) throws IOException {
        // 1. 构造数据
        Map<String, Object> data = new HashMap<>();
        data.put("name", "张三");
        data.put("age", 29);
        data.put("department", "技术部");

        // 图片（支持本地/网络图片）
        data.put("watermelon", "http://x/lemon.png");
        data.put("lemon", Pictures.ofLocal("sob.jpeg").size(24, 24).create());

        data.put("table", Tables.of(new String[][] {
                new String[] { "Song name", "Artist" }
        }).border(BorderStyle.DEFAULT).create());

        data.put("list", Numberings.create("Plug-in grammar",
                "Supports word text, pictures, table...",
                "Template, not just template, but also style template"));

        // 2. 渲染模板
        XWPFTemplate template = XWPFTemplate.compile(
                ResourceUtils.getFile("classpath:templates/template.docx")
        ).render(data);

        // 3. 导出下载
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment;filename=export.docx");
        template.write(response.getOutputStream());
        template.close();
    }
}
